//
//  NewsTableView.m
//  Mtime
//
//  Created by Mac on 15/8/7.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "NewsTableView.h"
#import "NewsModel.h"
#import "NewsCell.h"

#import "NewsViewController.h"
#import "ImageNewsViewController.h"

@implementation NewsTableView

//- (id)initWithFrame:(CGRect)frame {
//
//    if (self = [super initWithFrame:frame]) {
//        
//    }
//
//    return self;
//}

//- (void)setDataList:(NSMutableArray *)dataList {
//
//    _dataList = dataList;
//
//    [self reloadData];
//}

#pragma mark UITableViewDataSource
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//
//    return _dataList.count;
//
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NewsCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    
    NewsCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
    
    if (!cell1) {
        
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
        cell1 = [[[NSBundle mainBundle] loadNibNamed:@"NewsCell" owner:nil options:nil] lastObject];
        
    }

    if (!cell2) {
        
        cell2 = [[[NSBundle mainBundle] loadNibNamed:@"NewsCell" owner:nil options:nil] objectAtIndex:0];
        
    }
    
    NewsModel *model = self.dataList[indexPath.row];
    if ([model.type isEqual:@1]) {
        cell1.model = model;
        return cell1;
        
    }else {
    
        cell2.model = model;
        return cell2;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NewsModel *model = self.dataList[indexPath.row];
    
    if ([model.type isEqual:@2]) {//进入文字新闻界面
        
        //调用viewController方法来返回视图所在的视图控制器 push到新的界面
        
        NewsViewController *newsVC = [[NewsViewController alloc] init];
        newsVC.view.backgroundColor = [UIColor redColor];
        
        newsVC.hidesBottomBarWhenPushed = YES;
        
        [self.viewController.navigationController pushViewController:newsVC animated:YES];
        
    }else {
    
        //1.获得要加载的故事版
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"ImageNewsStoryboard" bundle:nil];
        
        //2.取出故事版中箭头指向的视图控制器:instantiateInitialViewController
        ImageNewsViewController *imageNewsVC = [storyboard instantiateInitialViewController];
        
        [self.viewController.navigationController pushViewController:imageNewsVC animated:YES];
    
    }
    

}



@end
